/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ace.system;

import java.util.Hashtable;
import ace.system.xml.XmlNode;
import ace.system.xml.XmlReaderWriter;

/**
 *
 * @author legACy
 */
public class StringManager {
	public static class Language {
		//Enum
		public static final Language ENGLISH	= new Language(0, "english");
		public static final Language INDONESIAN = new Language(1, "indonesia");
		
		//Protected constructor
		protected Language(int id, String name) {
			//Save
			m_ID	= id;
			m_Name	= name;
		}
		
		//Accessor
		public String getName()						{	return m_Name;									}
		public static Language[] getLanguages()		{	return new Language[] { INDONESIAN, ENGLISH };	}
		
		//Data
		protected int		m_ID;
		protected String	m_Name; 
	}
	
	//Inaccessible constructor
	protected StringManager() {
		//Initialize
		m_Tag			= "";
		m_Language		= null;
		m_StringTable	= new Hashtable();
	}
	
	public static StringManager instance() {
		//Create if null
		if (s_Instance == null) {
			s_Instance = new StringManager();
			s_Instance.loadString(Language.getLanguages()[1]);
		}
		return s_Instance;
	}
	
	public String getTag() {
		return m_Tag;
	}
	
	public Language getLanguage() {
		return m_Language;
	}
	
	public String getString(String key) {		
		//Find string in table
		return (String) m_StringTable.get(key);		
	}
	
	public void loadString(Language lang) {
		//Skip if no lang
		if (lang == null) return;
		
		//Reset table
		m_StringTable = new Hashtable();
		
		//Load xml
		String Filename = Utilities.STRING_FOLDER + "/" + lang.getName().toLowerCase() + ".xml";
		XmlNode XML		= XmlReaderWriter.getInstance().readFromXmlFile(Filename);
		
		//Get tag
		m_Tag = XML.getAttribute("tag");
		
		//For each child
		for (int i = 0; i < XML.getChildren().size(); i++) {
			//Get child
			XmlNode Child = (XmlNode)XML.getChildren().elementAt(i);
			
			//Save name and value
			String Name		= Child.getNodeName();
			String Value	= ((XmlNode)Child.getChildren().elementAt(0)).getNodeValue();
			m_StringTable.put(Name, Value);
		}
		
		//Save language
		m_Language = lang;
	}
	
	//Instance
	protected static StringManager s_Instance = null;
	
	//Data
	protected String	m_Tag;
	protected Language	m_Language;
	protected Hashtable m_StringTable;
}
